﻿<%@ WebHandler Language="C#" Class="RTGS.Web.ajaxhandler.rtgs.stp_record_detail_i_incoming" %>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using RTGS.Library.Enum;
using RTGS.Library.DataAccess.Rtgs;
using RTGS.Library.Entities.Rtgs;
using RTGS.Web.support;
using System.Configuration;
using RTGS.Library.DataAccess.UserManagement;
using RTGS.Library.Entities.UserManagement;
namespace RTGS.Web.ajaxhandler.rtgs
{

public class stp_record_detail_i_incoming : BaseHandler
    {
       private stpRecordDetailIRepo stpRecordDal = new stpRecordDetailIRepo(ConfigurationManager.ConnectionStrings["Rtgs"].ConnectionString);

       private sysTLogRepo logDal = new sysTLogRepo(ConfigurationManager.ConnectionStrings["Rtgs"].ConnectionString);
       private sysTLog log = new sysTLog();
       
        private string MethodName = string.Empty;

        public override void ProcessRequest(HttpContext context)
        {
            base.ProcessRequest(context);

            MethodName = context.Request.Params["method"];
            
            context.Response.Clear();
            context.Response.Buffer = true;

            switch (MethodName.ToLower())
            {
                    case ("getdata"):
                    context.Response.Write(GetData(context));
                    break;
                default:
                    // do nothing
                    break;
            }

            context.Response.Flush();
            context.Response.End();

        }

        public override bool IsReusable
        {
            get
            {
                return false;
            }
        }

        #region CRUD Handler





        private string GetData(HttpContext context)
        {
            int page = 1;
            int.TryParse(context.Request.QueryString["page"] ?? "1", out page);
            int pageSize = 10;
            int.TryParse(context.Request.QueryString["rows"] ?? "10", out pageSize);
            string sidx = ((context.Request.QueryString["sidx"]) == null ? stpRecordDetailI.Fields.recordDetailItrRelatedtrn : context.Request.QueryString["sidx"].ToString());
            string sord = ((context.Request.QueryString["sord"]) == null ? "ASC" : context.Request.QueryString["sord"].ToString());
            string parameters = string.Empty;

            string valuedate = ((context.Request.Params["valuedate"]) == null ? string.Empty : context.Request.QueryString["valuedate"].ToString());
            string trn = ((context.Request.Params["trn"]) == null ? string.Empty : context.Request.QueryString["trn"].ToString());
            string batchref = ((context.Request.Params["batchref"]) == null ? string.Empty : context.Request.QueryString["batchref"].ToString());
            string status = ((context.Request.Params["status"]) == null ? string.Empty : context.Request.QueryString["status"].ToString());
            string refno = ((context.Request.Params["refno"]) == null ? string.Empty : context.Request.QueryString["refno"].ToString());
            string tomember = ((context.Request.Params["tomember"]) == null ? string.Empty : context.Request.QueryString["tomember"].ToString());


            parameters = !string.IsNullOrEmpty(valuedate) ?
       (!string.IsNullOrEmpty(parameters) ? parameters + " AND RECORD_DETAIL_ITR_ValueDate LIKE '%" + valuedate + "%'" : " WHERE RECORD_DETAIL_ITR_ValueDate LIKE '%" + valuedate + "%'")
                     : parameters;

            parameters = !string.IsNullOrEmpty(trn) ?
                   (!string.IsNullOrEmpty(parameters) ? parameters + " AND RECORD_DETAIL_ITR_TRN LIKE '%" + trn + "%'" : " WHERE RECORD_DETAIL_ITR_TRN LIKE '%" + trn + "%'")
                                 : parameters;

            parameters = !string.IsNullOrEmpty(batchref) ?
                   (!string.IsNullOrEmpty(parameters) ? parameters + " AND RECORD_DETAIL_IRECORD_IH_BatchReference LIKE '%" + batchref + "%'" : " WHERE RECORD_DETAIL_IRECORD_IH_BatchReference LIKE '%" + batchref + "%'")
                                 : parameters;


            parameters = !string.IsNullOrEmpty(status) ?
                   (!string.IsNullOrEmpty(parameters) ? parameters + " AND RECORD_DETAIL_IST_Status LIKE '%" + status + "%'" : " WHERE RECORD_DETAIL_IST_Status LIKE '%" + status + "%'")
                                 : parameters;

            //parameters = !string.IsNullOrEmpty(refno) ?
            //       (!string.IsNullOrEmpty(parameters) ? parameters + " AND RECORD_DETAIL_ORECORD_RefNo LIKE '%" + status + "%'" : " WHERE RECORD_DETAIL_ORECORD_RefNo LIKE '%" + refno + "%'")
            //                     : parameters;

            parameters = !string.IsNullOrEmpty(tomember) ?
                   (!string.IsNullOrEmpty(parameters) ? parameters + " AND RECORD_DETAIL_ITR_ToMember LIKE '%" + tomember + "%'" : " WHERE RECORD_DETAIL_ITR_ToMember LIKE '%" + tomember + "%'")
                                 : parameters;
            
            int totalCount = Convert.ToInt32(stpRecordDal.SelectScalar(SQL.Function.Aggregate.Count, stpRecordDetailI.Fields.recordDetailItrRelatedtrn, parameters));
            decimal dc = decimal.Ceiling(Convert.ToDecimal(totalCount) / Convert.ToDecimal(pageSize));
            int totalPages = Convert.ToInt32(dc);
            List<stpRecordDetailI> pagedList = stpRecordDal.GetList(pageSize, page, sidx, sord, parameters);

            var query = from incoming in pagedList
                        //join product in productDTO on subproduct.productId equals product.id
                        select new
                        {
                            incoming.recordDetailItrRelatedtrn,
                            incoming.recordDetailItrValuedate,
                            incoming.recordDetailIstStatus,
                            incoming.recordDetailIstRsncde,
                            incoming.recordDetailItrTomember,
                            incoming.recordDetailItrTrn,
                            recordDetailItrAmount = incoming.recordDetailItrAmount == string.Empty ? string.Empty : Convert.ToDecimal(incoming.recordDetailItrAmount).ToString("#,###.00")


                        };
            
            if (!string.IsNullOrEmpty(stpRecordDal.GetErrorMessage()))
            {

                log = new sysTLog();
                log.logEvent = "GetData STP_RECORD_DETAIL_I_INCOMING Success";
                log.logMessage = string.Empty;

            }
            else
            {
                log = new sysTLog();
                log.logEvent = "View Data STP_RECORD_DETAIL_I_INCOMING  Error";
                log.logMessage = string.Empty;
            }

            log.logSource = "stp_record_detail_i_incoming.ashx";
            log.createdBy = base.UserLoggedIn;
            log.createdDate = DateTime.Now;
            logDal.Save(log);
            
            // send params into jqGrid's format
            var gridData = new
            {
                page = page,
                total = totalPages,
                records = totalCount,
                rows = query,

            };

            return Newtonsoft.Json.JsonConvert.SerializeObject(gridData);
        }

        #endregion


    }
}
